package com.evildog.workspace.life.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.DiaryBO;
import com.evildog.workspace.life.application.bo.DiaryPageBO;
import com.evildog.workspace.life.application.convert.DiaryConvert;
import com.evildog.workspace.life.application.dto.DiaryDTO;
import com.evildog.workspace.life.application.dto.DiaryPageDTO;
import com.evildog.workspace.life.application.entity.Diary;
import com.evildog.workspace.life.application.entity.DiaryPageDO;
import com.evildog.workspace.life.application.mapper.DiaryMapper;
import com.evildog.workspace.life.application.service.DiaryService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class DiaryServiceImpl extends ServiceImpl<DiaryMapper, Diary> implements DiaryService {

    @Override
    public PagedResult<DiaryPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, DiaryPageDTO pageDTO) {
        IPage<DiaryPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);
        return DiaryConvert.INSTANCE.convert(page);

    }

    @Override
    public DiaryBO getDetailById(Long id) {
        Diary diary = this.getById(id);
        Assert.notNull(diary, "diary is null!");
        return DiaryConvert.INSTANCE.convert(diary);
    }

    @Override
    public boolean save(DiaryDTO dto){
        Diary newDiary = DiaryConvert.INSTANCE.convert(dto);
        return save(newDiary);
    }

    @Override
    public boolean updateById(Long id, DiaryDTO dto){
        Diary oldDiary = getById(id);
        Assert.notNull(oldDiary, "diary is null!");
        Diary newDiary = DiaryConvert.INSTANCE.convert(dto);
        newDiary.setId(id);
        return updateById(newDiary);
    }
}
